package com.sky.ContactManager;


import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sky.ContactManagerbase.Abonnee;
import com.sky.ContactManagerbase.Address;
import com.sky.ContactManagerbase.Company;
import com.sky.ContactManagerbase.Contact;
import com.sky.ContactManagerbase.Email;
import com.sky.ContactManagerbase.Messenger;
import com.sky.ContactManagerbase.Tel;
import com.sky.ContactManagerbase.Website;

import android.content.Context;
import android.util.Log;



public class Json_Parser {
	public Abonnee parse(Context context,JSONObject jObject) throws JSONException
	{  
		//Liste_Produits group = new Liste_Produits();
		
		Log.d(this.getClass().toString(),"20" );
		//Log.d(this.getClass().toString(),String.valueOf(jArray.length()) );
		if (jObject.length()!=0)
		{
			Abonnee abonnee=new Abonnee(jObject.getInt("idabonnee"),jObject.getString("nom"),jObject.getString("prenom"),
					jObject.getString("login"),jObject.getString("motdepasse"),jObject.getString("email"),
					jObject.getString("code"));
			
			// On instancie une nouvelle liste de produits dans
			// notre groupe de prodiuts
			//group.produits = new ArrayList<Produit>();
			
			//group.nombre = jArray.length();
			//Log.d("group.nombre",String.valueOf(group.nombre));
			// On parse le groupe de produit
			JSONArray jlistcontact=jObject.getJSONArray("contact");
			for (int l = 0;l < jlistcontact.length();l++)
			{
				JSONObject jcontact = jlistcontact.getJSONObject(l);
				Contact contact=new Contact(jcontact.getInt("idcontact"),jcontact.getString("firstname"),
						jcontact.getString("lastname"),jcontact.getString("middlename"),jcontact.getString("nameprefix")
						,jcontact.getString("namesuffix"),jcontact.getString("phoneticgivenname"),
						jcontact.getString("phoneticfamilyname"),jcontact.getString("notes"),jcontact.getString("nickname")
						,jcontact.getString("internetcall"),jcontact.getString("photo"),jcontact.getString("jobtitle"),
						jcontact.getString("department"),jcontact.getInt("fk_abonnee"));
				
				JSONArray jlistemail=jcontact.getJSONArray("email");
				for(int m=0;m<jlistemail.length();m++)
				{
					JSONObject jemail = jlistemail.getJSONObject(m);
					Email email=new Email(jemail.getInt("idemail"),jemail.getString("mail"),jemail.getString("label")
							,jemail.getInt("fk_contact"));
					contact.email.add(email);
				}
				JSONArray jlistaddress=jcontact.getJSONArray("address");
				for(int n=0;n<jlistaddress.length();n++)
				{
					JSONObject jaddress=jlistaddress.getJSONObject(n);
					Address address=new Address(jaddress.getInt("idaddress"),jaddress.getString("label"),
							jaddress.getString("street"),jaddress.getString("city"),jaddress.getString("state")
							,jaddress.getString("country"),jaddress.getString("zip"),jaddress.getString("pobox"),
							jaddress.getString("neighborhood"),jaddress.getInt("fk_contact"));
					contact.address.add(address);
				}
				
				JSONArray jlistweb =jcontact.getJSONArray("website");
				for(int i=0;i<jlistweb.length();i++)
				{
					JSONObject jweb=jlistweb.getJSONObject(i);
					Website web=new Website(jweb.getInt("idwebsite"),jweb.getString("label"),jweb.getString("website")
							,jweb.getInt("fk_contact"));
					contact.web.add(web);
				}
				JSONArray jlisttel =jcontact.getJSONArray("tel");
				for(int j=0;j<jlisttel.length();j++)
				{
					JSONObject jtel=jlisttel.getJSONObject(j);
					Tel tel=new Tel(jtel.getInt("idtel"),jtel.getString("numero"),jtel.getString("label")
							,jtel.getInt("fk_contact"));
					contact.tel.add(tel);
				
				}
				
				JSONArray jlistcomp =jcontact.getJSONArray("company");
				for(int k=0;k<jlistcomp.length();k++)
				{
					JSONObject jcomp=jlistcomp.getJSONObject(k);
				Company comp=new Company(jcomp.getInt("idcompany"),jcomp.getString("label"),jcomp.getString("company")
						,jcomp.getInt("fk_contact"));
				contact.company.add(comp);
				}
				
				
				JSONArray jlistmess =jcontact.getJSONArray("messenger");
				for(int k=0;k<jlistmess.length();k++)
				{
					JSONObject jmess=jlistmess.getJSONObject(k);
					Messenger mess=new Messenger(jmess.getInt("idmessenger"),jmess.getString("label")
							,jmess.getString("im"),jmess.getInt("fk_contact"));
					contact.messenger.add(mess);
				}				
				
				abonnee.contact.add(contact);
			}
			return abonnee;
		}
		
		return null;
	//return group;
	}
	

}


